/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.supinfo.youfood.dao.jpa;

import com.supinfo.youfood.dao.MenuDao;
import com.supinfo.youfood.entity.Menu;
import com.supinfo.youfood.entity.Produit;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author Michael
 */
@Stateless
public class JpaMenuDao implements MenuDao{
    
    @PersistenceContext
    EntityManager em;
    
    @Override
    public Menu ajouterMenu (Menu m) {
        em.persist(m);
        return m;
    }
    
    @Override
    public void supprimerMenu (Menu m) {
        em.find(Menu.class, m);
        em.remove(m);
    }
    
    @Override
    public List<Menu> getAllMenus() {
        String query = "SELECT m "+
                        "FROM Menu m";
        Query result = em.createQuery(query);
        return (List<Menu>)result.getResultList();
    }

    @Override
    public Menu findMenuById(Long id) {
        return em.find(Menu.class, id);
    }

    @Override
    public Menu refreshMenu(Menu menu) {
        return em.merge(menu);
    }

    @Override
    public List<Menu> getAllActifMenus() {
        String query = "SELECT m "+
                        "FROM Menu m "+
                        "WHERE m.actif = 1";
        Query result = em.createQuery(query);
        return (List<Menu>)result.getResultList();
    }
    
}
